<?php
include('./config.php');
require './json.class.php';
require './Application/Home/Common/function.php';
use think\Log;
function unzipIpaFile($ipaFile)
{
	\PHP_TOT_OTAServer\unzip($ipaFile, "Temp/UNZIP/");
	return "Temp/UNZIP/";
}

function CreateIphonePlist($ctlist){
	//added by cq qq:852986690
	$title = $ctlist['title'];
	$subtitle = $ctlist['subtitle'];
	$versionname = $ctlist['versionname'];
	$bundle_identifier = $ctlist['bundle_identifier'];
	$ssl_server = $ctlist['ssl_server'];
	$channelid = $ctlist['cid'];
	$iconurl = $ctlist['icon'];
	header('Content-Type: application/xml');
	$plist = new XmlWriter();
	$plist->openMemory();
	$plist->setIndent(TRUE);
	$plist->startDocument('1.0', 'UTF-8');
	$plist->writeDTD('plist', '-//Apple//DTD PLIST 1.0//EN', 'http://www.apple.com/DTDs/PropertyList-1.0.dtd');
	$plist->startElement('plist');
	$plist->writeAttribute('version', '1.0');

	$pkg = array();
	$pkg['kind'] = 'software-package';
	$pkg['url'] = $channelid;

	$displayimage = array();
	$displayimage['kind'] = 'display-image';
	$displayimage['needs-shine'] = TRUE;
	$displayimage['url'] = $iconurl;

	$fullsizeimage = array();
	$fullsizeimage['kind'] = 'full-size-image';
	$fullsizeimage['needs-shine'] = TRUE;
	$fullsizeimage['url'] = $iconurl;

	$assets = array();
	$assets[] = $pkg;
	$assets[] = $displayimage;
	$assets[] = $fullsizeimage;

	$metadata = array();
	$metadata['bundle-identifier'] = $bundle_identifier;
	$metadata['bundle-version'] = $versionname;
	$metadata['kind'] = 'software';
	$metadata['subtitle'] = $subtitle;
	$metadata['title'] = $title;

	$items0 = array();
	$items0['assets'] = $assets;
	$items0['metadata'] = $metadata;

	$items = array();
	$items[] = $items0;

	$root = array();
	$root['items'] = $items;

	xmlWriteValue($plist, $root);

	$plist->endElement();
	$plist->endDocument();
	return $plist->outputMemory();
	//added by cq qq:852986690
}


//$_POST['uid'] = '4180';
//$_POST['iAppSize'] = '5000';
//$_POST['name'] = '2017-07-18/596dcb6a83fab.ipa';
//$_POST['iLocalId'] = '1186';
$iLocalId = $_POST['iLocalId'];
$uid = $_POST['uid'];
$iAppSize = $_POST['iAppSize'];
//$_GET['dafufile'] = '2017-07-12/nc.apk';
$dafufile = trim($_POST['name']);

$dafufilearr = explode("/",$dafufile);
$apkfilename = $dafufilearr[1];
$dafupng = explode(".",$dafufilearr[1]) ;
$ext = strtolower($dafupng[1]);

if($ext=='apk'){

		$data = array();
		$data['flag'] = '0';

		$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
		//$data['flag'] = 'a1';
		$file = './Public/uploads/'.$dafufile;
		$mmfilenamearr = explode('/',$dafufile);
		$mmfilename = end($mmfilenamearr);
		$remote_file = '/public_html/upload/'.$mmfilename; // 注意：这里的"/test"指的是ftp根目录下的test文件夹，而非系统跟目录下的test
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
		if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
			$flag = 'ok';
		} else {
			$flag = 'error';
		}
		ftp_close($conn_id);

		$iphonebundleid = '';//app bundleId add by cq qq:852986690
		$CFBundleName = '';//app name  add by cq qq:852986690
		$CFBundleShortVersionString = ''; //bundle version add by cq qq:852986690
		$zxlogo = date("Y-m-d").'/'.substr(uniqid(),1,10).'/icon.png';
		$iconAddress = __DIR__.'/Public/icon/'.$zxlogo;
		$ipaloalfile =  __DIR__.'/Public/uploads/'.$dafufile;
		$appResultInfo =  GetAPPpackageInfo($ipaloalfile,$iconAddress);

		$iphonebundleid = $appResultInfo->BundleId;
		$CFBundleName 	 =$appResultInfo->AppName;
		$CFBundleShortVersionString  = $appResultInfo->BundleVersion; //版本号

		$con = mysql_connect("localhost",$dbuser,$dbpass);
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
			mysql_query("set names 'utf8'",$con);
		mysql_select_db($dbname, $con);

		$shorturl = urlShort($iLocalId);

		$fid = $uid;
		$web = '/a/'.$shorturl;
		$uptxt = $server_url.$apkfilename;
		$create_time = time();
		$ext = $ext;
		$zsize = $iAppSize;
		$shorturl = $shorturl;
		$mmtype = '1';
		$isgg = '2';

		mysql_query("update lc_list set nickname='".$CFBundleName."', fid='".$fid."', web='".$web."', uptxt='".$uptxt."', create_time='".$create_time."', ext='".$ext."', zsize='".$zsize."', zxlogo='".$zxlogo."', shorturl='".$shorturl."', mmtype='".$mmtype."',isgg='".$isgg."' where id='".$iLocalId."'");

		mysql_close($con);
		$data['flag'] = '1';
		$data['id'] = $iLocalId;
		$json = new Services_JSON;
		echo $json->encode($data);
		exit;
}else{

	$data = array();
	$data['flag'] = '0';

	  $targetFile =  "./Public/uploads/".$dafufile;
		require_once './util/Classes/TOTClasses/unzip.php';
		require_once './util/Classes/TOTClasses/HandleInfoPlistInPayload.php';
		require_once './util/Classes/TOTClasses/FileSystemHelper.php';

		$iphonebundleid = '';//app bundleId add by cq qq:852986690
		$CFBundleName = '';//app name  add by cq qq:852986690
		$CFBundleShortVersionString = ''; //bundle version add by cq qq:852986690
		$zxlogo = date("Y-m-d").'/'.substr(uniqid(),1,10).'/icon.png';
		$iconAddress = __DIR__.'/Public/icon/'.$zxlogo;
		$ipaloalfile =  __DIR__.'/Public/uploads/'.$dafufile;
		$appResultInfo =  GetAPPpackageInfo($ipaloalfile,$iconAddress);

		$iphonebundleid = $appResultInfo->BundleId;
		$CFBundleName 	 =$appResultInfo->AppName;
		$CFBundleShortVersionString  = $appResultInfo->BundleVersion; //版本号

		$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
		$file = './Public/uploads/'.$dafufile;
		$mmfilenamearr = explode('/',$dafufile);
		$mmfilename = end($mmfilenamearr);
		$remote_file = '/public_html/upload/'.$mmfilename; // 注意：这里的"/test"指的是ftp根目录下的test文件夹，而非系统跟目录下的test
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

		if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
			$flag = 'ok';
		} else {
			$flag = 'error';
		}
		ftp_close($conn_id);


		$con = mysql_connect("localhost",$dbuser,$dbpass);
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
		mysql_query("set names 'utf8'",$con);
		mysql_select_db($dbname, $con);

		$shorturl = urlShort($iLocalId);

		$newFile='default.png';
		$appname = '';

		$hpackageName = '';
		$hversion = '1.0';
			$uptxt = $server_url.$apkfilename;
		//added by cq qq:852986690
		$ctlist = array(
				'ssl_server' => 'http://' . $_SERVER['SERVER_NAME'] . '/',
				'bundle_identifier' => $iphonebundleid,
				'title' => $CFBundleName ,
				'cid' => $uptxt,
				'subtitle' => $CFBundleName ,
				'versionname' => $CFBundleShortVersionString,
				'icon' => 'https://download.qq6789.com'.'/Public/icon/'.$zxlogo,
		);

		$fp = fopen('./Public/appipa/'.$iLocalId.".plist", "w+");
		$streams = CreateIphonePlist($ctlist);
		fwrite($fp, $streams );
		fclose($fp);

		$nickname = $appname;
		$fid = $uid;
		$web = '/a/'.$shorturl;
		$create_time = time();
		$ext = $ext;
		$zsize = $iAppSize;
		$shorturl = $shorturl;
		$mmtype = '1';
		$isgg = '2';
		$plistdb = 'itms-services://?action=download-manifest&url='.'https://'.'download.qq6789.com/'.$iLocalId.".plist";
		mysql_query("update lc_list set nickname='".$CFBundleName."',plist='".$plistdb."', fid='".$fid."', iphonebundleid='".$iphonebundleid."',bundleversion='".$CFBundleShortVersionString."', web='".$web."', uptxt='".$uptxt."', create_time='".$create_time."', ext='".$ext."', zsize='".$zsize."', zxlogo='".$zxlogo."', shorturl='".$shorturl."', mmtype='".$mmtype."', isgg='".$isgg."' where id='".$iLocalId."'");

		mysql_close($con);
		$data['flag'] = '1';
		$data['id'] = $iLocalId;

		//$data['CFBundleName'] = $CFBundleName;
		//$data['iphonebundleid'] = $iphonebundleid;
		//$data['bundleversion'] = $CFBundleShortVersionString;
		$json = new Services_JSON;
		echo $json->encode($data);
		exit;


}


?>
